package tech.heptagram.staff.tools.service;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.core.common.rest.RestResultEnum;
import tech.heptagram.staff.exception.StaffApiException;
import tech.heptagram.staff.model.Department;
import tech.heptagram.staff.query.DepartmentQuery;
import tech.heptagram.staff.query.DepartmentQueryBuilder;
import tech.heptagram.staff.query.DepartmentRemoveReq;
import tech.heptagram.staff.query.DepartmentReq;
import tech.heptagram.staff.service.DepartmentService;

/**
 * @author wang.yuchuan
 * @date 2018/9/10
 */
@Slf4j
public class DepartmentServiceRestTemplateImpl extends AbstractServiceImpl implements DepartmentService {

    private RestTemplate restTemplate;

    public DepartmentServiceRestTemplateImpl(String staffApiDomain, RestTemplate restTemplate) {
        super(staffApiDomain);
        this.restTemplate = restTemplate;
    }

    @Override
    public Department getById(Integer id) {
        log.info("请求根据部门id获取部门信息接口getById: {}", id);

        String url = getStaffApiDomainUrl() + "/api/v3/department/get/" + id;

        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(url))
            .accept(MediaType.APPLICATION_JSON)
            .build();

        ParameterizedTypeReference<ResponseResult<Department>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<Department>>() {
        };
        ResponseEntity<ResponseResult<Department>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求根据部门id获取部门信息接口getById:返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    /**
     * 根据父部门ID查询
     *
     * @param parentId 父部门ID
     * @return 部门列表信息
     */
    @Override
    public List<Department> getByParentId(Integer parentId) {
        log.info("请求根据父部门id获取部门信息接口getById: {}", parentId);

        String url = getStaffApiDomainUrl() + "/api/v3/department/get/parentId/" + parentId;

        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .build();

        ParameterizedTypeReference<ResponseResult<List<Department>>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<List<Department>>>() {
        };
        ResponseEntity<ResponseResult<List<Department>>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求根据父部门id获取部门信息接口getById:返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public List<Department> findByQuery(DepartmentQuery departmentQuery) {
        log.info("请求根据查询条件获取部门信息接口findByQuery: {}", departmentQuery);

        String url = getStaffApiDomainUrl() + "/api/v3/department/find";

        RequestEntity<DepartmentQuery> requestEntity = RequestEntity.post(URI.create(url))
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .body(departmentQuery);

        ParameterizedTypeReference<ResponseResult<List<Department>>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<List<Department>>>() {
        };
        ResponseEntity<ResponseResult<List<Department>>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求根据查询条件获取部门信息接口findByQuery:返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public List<Department> findByIds(List<Integer> ids) {
        DepartmentQuery departmentQuery = DepartmentQueryBuilder.builder().departmentIds(ids).build();
        return findByQuery(departmentQuery);
    }

    @Override
    public List<Department> findByStaffCode(String staffCode) {
        log.info("请求根据员工编码获取部门信息接口 findByStaffCode: {}", staffCode);

        String url = getStaffApiDomainUrl() + "/api/v3/department/get/staff/" + staffCode;

        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(url))
            .accept(MediaType.APPLICATION_JSON)
            .build();

        ParameterizedTypeReference<ResponseResult<List<Department>>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<List<Department>>>() {
        };
        ResponseEntity<ResponseResult<List<Department>>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求根据员工编码获取部门信息接口 findByStaffCode:返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public Department findByDingDepartmentId(Long dingDepartmentId) {
        log.info("请求 根据钉钉部门id获取部门信息 接口: {}", dingDepartmentId);

        String url = getStaffApiDomainUrl() + "/api/v3/department/get/dingtalk/" + dingDepartmentId;

        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .build();

        ParameterizedTypeReference<ResponseResult<Department>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<Department>>() {
        };
        ResponseEntity<ResponseResult<Department>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 根据钉钉部门id获取部门信息 接口:返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public Integer save(DepartmentReq req) {
        log.info("请求 保存部门信息 接口: {}", req);

        String saveDeptUrl = getStaffApiDomainUrl() + "/api/v3/department/save";

        RequestEntity<DepartmentReq> requestEntity = RequestEntity.post(URI.create(saveDeptUrl))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(req);

        ParameterizedTypeReference<ResponseResult<Integer>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<Integer>>() {
        };
        ResponseEntity<ResponseResult<Integer>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 保存部门信息接口 返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public void update(DepartmentReq departmentReq) {
        log.info("请求 修改部门信息 接口: {}", departmentReq);

        String updateDeptUrl = getStaffApiDomainUrl() + "/api/v3/department/update";

        RequestEntity<DepartmentReq> requestEntity = RequestEntity.post(URI.create(updateDeptUrl))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(departmentReq);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 修改部门信息接口 返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public void delete(DepartmentRemoveReq departmentReq) {
        log.info("请求 删除部门信息 接口: {}", departmentReq);

        String DeleteDeptUrl = getStaffApiDomainUrl() + "/api/v3/department/delete";

        RequestEntity<DepartmentRemoveReq> requestEntity = RequestEntity.post(URI.create(DeleteDeptUrl))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(departmentReq);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 删除部门信息接口 返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }


}
